rm(list = ls())
library(tidyr)
library(readr)
library(dplyr)
library(readxl)
library(tidyr)
library(arm)
library(lme4)
library(MASS)
library(texreg)
library(ggplot2)
load("Data/paperdata.RData") #1993-2006

source("Code/coefplot.R")
summary(paperdata)

data <- paperdata %>% dplyr::select(conflict_id, dyad_id, year, ext_sup, ext_sup_gov,
                                    ext_sup_rebel)

data1 <- data %>% dplyr::group_by(year) %>% 
      dplyr::summarise( ext_sup = sum( ext_sup),
                        ext_sup_gov = sum(ext_sup_gov),
                        ext_sup_rebel = sum(ext_sup_rebel),
                        dyads = n())

data1 <- data1 %>% 
    dplyr::mutate(ext_sup = ext_sup/dyads*100,
                  ext_sup_gov = ext_sup_gov/dyads*100,
                  ext_sup_rebel = ext_sup_rebel/dyads*100)

ggplot(data = data1, aes(x = year)) + 
  geom_line(aes(y =ext_sup, linetype = "dotted"),color="black",linewidth=1) +
  geom_line(aes(y = ext_sup_gov, linetype="twodash"), color="black", linewidth=1) + 
  geom_line(aes(y = ext_sup_rebel,linetype="solid"), color="black", linewidth=1)+
  theme_bw() +
  scale_linetype_manual(name = "", values = c("dotted" , "twodash",  "solid"  ),
                        labels = c("外部支持","外部支持反叛组织","外部支持政府")) +
  scale_x_continuous(breaks = seq(1992, 2017, by = 3))+
  xlab("年份") + ylab("占冲突对子的百分比(%)") +
  theme(legend.position="bottom",
        legend.title=element_blank(),
        legend.text = element_text(colour="black", size=16),
        axis.text= element_text(size=16),
        text = element_text(size=16, family = 'KaiTi',face = "bold"),
        plot.title = element_text(hjust = .5, size = 16))+
  annotate("segment", x = 1993, xend = 1993, y = 80, yend = 75,
           colour="black", arrow=arrow(length=unit(0.2,"cm"))) + 
  annotate("text", x = 1993, y = 80, parse = F, 
           label = "外部支持",size=5,family = 'KaiTi') +
  annotate("segment", x = 2014, xend = 2013, y = 52, yend = 47,
           colour="black", arrow=arrow(length=unit(0.2,"cm"))) + 
  annotate("text", x = 2015, y = 53, parse = F, 
           label = "外部支持反叛组织",size=5,family = 'KaiTi') +
  annotate("segment", x = 2000, xend = 1999, y = 38, yend = 42,
           colour="black", arrow=arrow(length=unit(0.2,"cm"))) + 
  annotate("text", x = 2002, y = 36, parse = F, 
           label = "外部支持政府",size=5,family = 'KaiTi') +
  theme(legend.position="bottom",
        legend.title=element_blank(),
        legend.text = element_text(colour="black", size=16),
        axis.text= element_text(size=16),
        text = element_text(size=16, family = 'KaiTi',face = "bold"),
        plot.title = element_text(hjust = .5, size = 16)) #+
    #ggtitle("外部力量支持内战方的趋势（1992-2017）")
ggsave("Results/figures/esd_trend.pdf", width = 8, height = 4)
ggsave("Replication/figures/esd_trend.bmp", width = 8, height = 4)
  
